package com.briup.mybatis01code.mapper;

import com.briup.mybatis01code.entity.ESUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.util.List;

@Mapper
@Component
public interface ESUserMapper {

    /**
     * 根据主键删除一行数据
     * @param id 主键
     */
    @Delete("delete from es_user where id=#{id}")
    void deleteById(Integer id);

    /**
     * 数据保存
     * @param user 一行数据对应的对象
     */
    @Insert("insert into es_user(id,username,password,phone,avatar,gender,status,birthday,register_time) values(seq_test1.nextval,#{username},#{password},#{phone},#{avatar},#{gender},#{status},#{birthday},#{registerTime})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void  save(ESUser user);

    /**
     * 根据主键修改其他信息
     * @param user
     */
    @Update("update es_user set username=#{username},password=#{password},phone=#{phone},avatar=#{avatar},gender=#{gender},status=#{status},birthday=#{birthday},register_time=#{registerTime} where id=#{id}")
    void updateUser(ESUser user);

    /**
     * 通过主键查询用户信息
     * @param id 主键
     * @return 用户对象
     */
    @Select("select *  from es_user where id=#{id}")
//    @Select("select id,username,password,phone,avatar,gender,status,birthday,register_time as registerTime from es_user  where id=#{id}")
    ESUser findById(Integer id);

    @Results(@Result(column = "register_time",property  ="rt"))
    @Select("select *  from es_user")
    List<ESUser> findAll();


    @Select("select  *  from  es_user where username like #{username} and status=#{status} and register_time between #{start} and #{end} order by register_time")
    List<ESUser>   findByCondition(String username, int status, @Param("start") Date startDate,@Param("end") Date endDate);


}
